home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / NTUMIN10.ARJ / COMPARE.C < prev    next >
C/C++ Source or Header  |  1992-03-12  |  6KB  |  177 lines

  1. /****************************************************************************
  2.  *
  3.  *    Program Name : COMPARE.C
  4.  *
  5.  *    Written By : Eng-Huat Ong and Kian-Mong Low.
  6.  *
  7.  *    This program checks the correctness of the solution array by
  8.  *     converting the input array and output array to minterms, sorting
  9.  *    them (ascending order) and check to see if they are equal.
  10.  *
  11.  *    Note : This program is usable ONLY for ON-array only data input.
  12.  *
  13.  * --------------------------------------------------------------------------
  14.  *    Copyright (c) 1992. All Rights Reserved. Nanyang Technological
  15.  *    University.
  16.  *
  17.  *    You are free to use, copy and distribute this software and its
  18.  *    documentation providing that:
  19.  *
  20.  *        NO FEE IS CHARGED FOR USE, COPYING OR DISTRIBUTION.
  21.  *
  22.  *        IT IS NOT MODIFIED IN ANY WAY.
  23.  *
  24.  *        THE COPYRIGHT NOTICE APPEAR IN ALL COPIES.
  25.  *
  26.  *    This program is provided "AS IS" without any warranty, expressed or
  27.  *    implied, including but not limited to fitness for any particular
  28.  *    purpose.
  29.  *
  30.  *    If you find NTUMIN fast, easy, and useful, a note or comment would be
  31.  *    appreciated. Please send to:
  32.  *
  33.  *        Boon-Tiong Tan or Othman Bin Ahmad
  34.  *        School of EEE
  35.  *        Nanyang Technological University
  36.  *        Nanyang Avenue
  37.  *        Singapore 2263
  38.  *        Republic of Singapore
  39.  *
  40.  ****************************************************************************
  41.  *
  42.  *    ma, mb    == no. of minterms in ON arrays, a & b
  43.  *    nb, nb    == no. of variables of ON arrays, a & b
  44.  *    nspm    == no. of storage/minterm (bytes/minterm)
  45.  *      m3    == total no. of bytes in array a or b
  46.  *    test    == hold status of memory comparison
  47.  *
  48.  ***************************************************************************/
  49.  
  50.  
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <string.h>
  54. #define mask8 255
  55.  
  56.  
  57. main()
  58.  
  59. {
  60.    unsigned short    m1, m2, ma, mb;
  61.    unsigned char     *infile, *outfile, *readf(), *sorting();
  62.    unsigned char     *a, *b, nspm, na, nb, c;
  63.          int     test;
  64.    unsigned long     m3;
  65.    FILE              *in, *out;            /*  file pointers */
  66.  
  67.    printf("This program is for checking the output generated by the minimization \n");
  68.    printf("program to verify that it is correct.\n\n");
  69.    printf("Before that, the output file must be converted to one similar to the\n");
  70.    printf("input file. (Not usable for inputs with DC array)\n\n");
  71.    printf("Press any key to Continue or <ESC> to QUIT\n\n");
  72.    c = getch();
  73.    if (c==27)
  74.       exit(0);
  75.  
  76.    infile = (unsigned char *)malloc (21);       /* space for 1st filename */
  77.    if (infile==0)
  78.       {
  79.      printf("Out of memory -- COMPARE, *infile\n");
  80.      printf("Program Terminated - 1\n");
  81.      exit(0);
  82.       }
  83.  
  84.    outfile = (unsigned char *)malloc (21);      /* space for 2nd filename */
  85.    if (outfile==0)
  86.       {
  87.      printf("Out of memory -- COMPARE, *outfile\n");
  88.      printf("Program Terminated - 2\n");
  89.      exit(0);
  90.       }
  91.  
  92.    printf("Enter input filename -> ");
  93.    gets(infile);                                 /* read 1st filename */
  94.  
  95.    printf("Enter output filename -> ");
  96.    gets(outfile);                                /* read 2nd filename */
  97.  
  98.    if ((in = fopen(infile, "r+")) == NULL)           /* open 1st file */
  99.       {
  100.      printf("Error opening file, %s\n", infile);
  101.      printf("Program terminated.\n ") ;
  102.      exit(0);
  103.       }
  104.  
  105.    if ((out = fopen(outfile, "r+")) == NULL)         /* open 2nd file */
  106.       {
  107.      printf("Error opening file, %s\n", outfile);
  108.      printf("Program terminated.\n ") ;
  109.      exit(0);
  110.       }
  111.  
  112.    /***
  113.     ***  READING FROM FILES AND CONVERTING TO MINTERMS
  114.     ***/
  115.  
  116.    printf("\nPlease Hang On, Reading From Files & Converting To Minterms ... \n\n");
  117.  
  118.    a = readf(in, a, 0);                /* read ON array, 1st file */
  119.    a = sorting(a);                     /* sort in ascending order */
  120.    fclose(in);                         /* close 1st file */
  121.  
  122.    na = *a;                            /* no. of variables */
  123.    m1 = *(a+1);
  124.    m2 = *(a+2);
  125.    ma = (m2&mask8)<<8 | m1;            /* no. of minterms */
  126.  
  127.    b = readf(out, b, 0);               /* read ON array, 2nd file */
  128.    b = sorting(b);                     /* sort in ascending order */
  129.    fclose(out);                        /* close 2nd file */
  130.  
  131.    nb = *b;                            /* no. of variables */
  132.    m1 = *(b+1);
  133.    m2 = *(b+2);
  134.    nspm = *(b+3);                      /* no. of byte/minterm */
  135.    mb = (m2&mask8)<<8 | m1;            /* no. of minterms */
  136.    m3 = mb*nspm+4;                     /* total no. of bytes */
  137.  
  138.    /***
  139.     ***  COMPARING MINTERMS
  140.     ***/
  141.  
  142.    printf("\nPlease Hang On, Comparing Minterms ... \n\n");
  143.  
  144.    if (na !=  nb)                      /* unequal no. of variables */
  145.       {
  146.      printf("Different number of variables in the 2 files. \n");
  147.      printf("Program Terminated - 3 \n");
  148.      exit(0);
  149.       }
  150.  
  151.    if (ma != mb)                      /* unequal no. of minterms */
  152.       {
  153.      printf("Different number of minterms produced from the 2 files.\n");
  154.      printf("Program Terminated - 4 \n");
  155.      exit(0);
  156.       }
  157.  
  158.    printf("Please Hang On, Doing comparison ... \n\n");
  159.  
  160.    test = memcmp(a, b, m3);           /* compare all minterms and header */
  161.  
  162.    if (test != 0)                     /* not exactly the same */
  163.       {
  164.      printf("Minterms produced from the 2 files are different .\n");
  165.      printf("Solution array obtained is incorrect. \n");
  166.      printf("Program Terminated - 5 \n");
  167.      exit(0);
  168.       }
  169.  
  170.    printf("Minterms produced from the files, %s and %s are equal.\n", infile, outfile);
  171.  
  172.    free(a);                          /* free pointers */
  173.    free(b);
  174.    free(infile);
  175.    free(outfile);
  176. }
  177.